package com.kx.talk.settings;

import com.kx.talk.mapper.UserFriendShipPolicyMapper;
import com.kx.talk.pojo.UserFriendShipPolicy;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 好友策略的配置信息类
 *
 * @Author Sun
 * @Date 2021/7/28 20:43
 */
@Component
public class FriendPolicySetting {

    @Resource
    private UserFriendShipPolicyMapper userFriendShipPolicyMapper;
    /**
     * 将好友策略的名称作为key，策略id作为value
     * 容器一启动时将数据从数据库查询出来放入List集合中
     */
    List<Map<String, Integer>> friendPolicySettings = new ArrayList<>();

    public static int POLICY_ID_1;
    public static int POLICY_ID_2;
    public static int POLICY_ID_3;
    public static int POLICY_ID_4;
    public static int POLICY_ID_5;

    public static final String POLICY_NAME_1 = "随意添加";
    public static final String POLICY_NAME_2 = "验证问题";
    public static final String POLICY_NAME_3 = "输入密码";
    public static final String POLICY_NAME_4 = "验证信息";
    public static final String POLICY_NAME_5 = "拒绝添加";


    @PostConstruct
    public void initSetting() {
        List<UserFriendShipPolicy> policies = userFriendShipPolicyMapper.selectByExample(null);
        for (UserFriendShipPolicy policy : policies) {
            HashMap<String, Integer> map = new HashMap<>();
            map.put(policy.getPolicyName(), policy.getId());
            friendPolicySettings.add(map);
        }

        for (Map<String, Integer> policySetting : friendPolicySettings) {
            if (policySetting.containsKey(POLICY_NAME_1)) {
                POLICY_ID_1 = policySetting.get(POLICY_NAME_1);
            }
            if (policySetting.containsKey(POLICY_NAME_2)) {
                POLICY_ID_2 = policySetting.get(POLICY_NAME_2);
            }
            if (policySetting.containsKey(POLICY_NAME_3)) {
                POLICY_ID_3 = policySetting.get(POLICY_NAME_3);
            }
            if (policySetting.containsKey(POLICY_NAME_4)) {
                POLICY_ID_4 = policySetting.get(POLICY_NAME_4);
            }
            if (policySetting.containsKey(POLICY_NAME_5)) {
                POLICY_ID_5 = policySetting.get(POLICY_NAME_5);
            }
        }
    }
}
